home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Utilities / GOCR / src / tga.cc < prev    next >
C/C++ Source or Header  |  1999-05-27  |  2KB  |  70 lines

  1. // Joerg Schulenburg Mai99
  2.  
  3. #include <stdio.h>
  4. // #include <stdlib.h>
  5. #include <assert.h>
  6.  
  7. #include "tga.h"
  8.  
  9. typedef unsigned char byte;
  10.  
  11. // --- needed for reading TGA-files
  12. #if 0
  13. char read_b(FILE *f1){    // filter #-comments
  14.   char c;
  15.   c=fgetc(f1); assert(!feof(f1)); assert(!ferror(f1));
  16.   return c;
  17. }
  18. #endif
  19.  
  20. //byte tga[18]={ 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,24,32};
  21. /*  header_hex= 00 00 02 00 00 00 00 00 00 00 00 00 xl xh yl yh  
  22.  *              18 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
  23.  
  24. void readtga(char *name,pix *p,int mode){     // see pcx.format.txt
  25.                         // mode: 0=gray,1=RGB
  26.   int nx,ny,i,x,y;
  27.   FILE *f1;  
  28.   unsigned char *pic,h[18];
  29.  
  30.   f1=fopen(name,"rb"); if(!f1) printf(" error opening file\n");
  31.   assert(f1);        // open-error
  32.   assert(fread(h,1,18,f1)==18);    /* 18 Byte lesen -> h[] */
  33.   assert(h[ 0]== 0);    // TGA0
  34.   assert(h[ 1]== 0);    // TGA1
  35.   assert(h[ 2]== 2);    // TGA2 no run length encoding
  36.   for(i=3;i<12;i++)
  37.   assert(h[ i]== 0);    // ???
  38.   assert(h[16]==0x18);    // TGA16
  39.   assert(h[17]==0x20);    // TGA17
  40.   nx = h[12] + (h[13]<<8);  /* x-dimension low high */
  41.   ny = h[14] + (h[15]<<8);  /* y-dimension low high */
  42.   printf("# TGA version=%d x=%d y=%d", h[2],nx,ny );
  43.   fflush(stdout);
  44.   // pic=new Uchar[ (*nx)*(*ny) ];        // errors!
  45.   pic=new unsigned char[ 3*nx*ny ];
  46.   assert(pic!=NULL);                // no memory
  47.   assert(ny==(int)fread(pic,3*nx,ny,f1));    // read all lines BGR
  48.   if(mode==0)
  49.   {
  50.     for(y=0;y<ny;y++)    /* BGR => gray */
  51.     for(x=0;x<nx;x++)
  52.     {  i=x+y*nx; pic[i]=(pic[i*3+0]+pic[i*3+1]+pic[i*3+2])/3; }
  53.   }
  54.   else
  55.   if(mode==1)
  56.   {
  57.     byte b;
  58.     for(y=0;y<ny;y++)    /* BGR => RGB */
  59.     for(x=0;x<nx;x++)
  60.     {  i=x+y*nx; b=pic[i*3+0]; pic[i*3+0]=pic[i*3+2]; pic[i*3+2]=b; }
  61.   }
  62.   else assert(0);        // wrong mode
  63.   fclose(f1);
  64.   p->p=pic;  p->x=nx;  p->y=ny; p->bpp=1+2*mode;
  65.   printf(" mode=%d\n",mode);
  66. }
  67.  
  68. // ------------------------------------------------------------------------
  69.  
  70.